<?php

/**
 * @author PhuTang, PhatPham
 * @copyright 2013
 */
class Surveymodel extends CI_Model {
    var $survey_id; //sid
    var $reused_survey_id;  //rsid
    var $survey_type_id;    //stid
    var $survey_name;   //sname
    var $course;    //course
    var $graduated_year;    //gyear
    var $is_graduated;  //isgrad
    var $start_date;    //sdate
    var $end_date;  //edate
    var $is_vocation;   //isvoc
    var $design_template_id;    //dtid
    var $title; //title
    var $status;    //status
    var $is_valuated;   //isval    
    var $is_deleted;
    var $created_by_user_id;    //userid
    var $created_on_date;   //date
    var $last_modified_by_user_id;  //userid
    var $last_modified_on_date; //date
    
    function __construct()
    {
        // Call the Model constructor
        $this->load->database();
        $this->load->helper('date'); //load date helper
    }
    //List-----------------------------------------------------------------------------------
	//Author: PhatPham
	//Get surveys list; $sid null -> get all, otherwise -> exclude $sid
    function get_surveys_list($stid, $sid)
    {
		if($sid == null){
			$query = $this->db->get_where('sur_survey',array('is_deleted' => false, 'survey_type_id'=>$stid));
			return $query;
		}else{
			$query = $this->db->get_where('sur_survey',array('is_deleted' => false, 'survey_type_id'=>$stid, 'survey_id !=' => $sid));
			return $query;
		}
    }
	//Author:PhatPham
    //Get survey by id
    function get_survey($sid){
        $query = $this->db->get_where('sur_survey', array('survey_id' => $sid, 'is_deleted' => false));
        return $query->num_rows() > 0 ? $query->row() : null;
    }
    
    //Create survey--------------------------------------------------------------------------------------
    function create_survey($sid, $rsid, $stid, $sname, $course, $gyear, $isgrad, $sdate, $edate, $isvoc, $dtid, $title, $status, $iseval, $userid){
        $data = array(
                'survey_id' => $sid,
                'reused_survey_id' => $rsid,
                'survey_type_id' => $stid,
                'survey_name' => $sname,
                'course' => $course,
                'graduated_year' => $gyear,
                'is_graduated' => $isgrad,
                'start_date' => $sdate,
                'end_date' => $edate,
                'is_vocation' => $isvoc,
                'design_template_id' => $dtid,
                'title' => $title,
                'status' => $status,
                'is_evaluated' => $iseval,
                'is_deleted' => false,
                'created_by_user_id' => $userid,
                'created_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->insert('sur_survey', $data);
    }
    //Delete survey--------------------------------------------------------------------------------------
    function delete_survey($sid, $userid){
        $data = array(
               'is_deleted' => true,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('survey_id', $sid);
        $this->db->update('sur_survey', $data);
    }
    //Edit survey----------------------------------------------------------------------------------------
    function edit_survey_1($sid, $rsid, $stid, $sname, $course, $gyear, $isgrad, $sdate, $edate, $isvoc, $iseval, $userid){
        $data = array(
                'reused_survey_id' => $rsid,
                'survey_type_id' => $stid,
                'survey_name' => $sname,
                'course' => $course,
                'graduated_year' => $gyear,
                'is_graduated' => $isgrad,
                'start_date' => $sdate,
                'end_date' => $edate,
                'is_vocation' => $isvoc,
                'is_evaluated' => $iseval,
                'last_modified_by_user_id' => $userid,
                'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('survey_id', $sid);
        $this->db->update('sur_survey', $data);
    }
    function edit_survey_2($sid, $dtid, $userid){
        $data = array(
               'design_template_id' => $dtid,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('survey_id', $sid);
        $this->db->update('sur_survey', $data);
    }
    function edit_survey_3($sid, $title, $userid){
        $data = array(
               'title' => $title,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('survey_id', $sid);
        $this->db->update('sur_survey', $data);
    }
    function edit_survey_status($sid, $status, $userid)
    {
        $data = array(
               'status' => $status,
               'last_modified_by_user_id' => $userid,
               'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->where('survey_id', $sid);
        $this->db->update('sur_survey', $data);
    }
    //Check whether survey duration is duplicated
    function check_survey_duration($sdate, $edate, $sid){
        $query = null;
        if($sid == null)
            $query = $this->db->query("select * from sur_survey where ((start_date <= '".$sdate."' and end_date >= '".$sdate."') or (start_date <= '".$edate."' and end_date >= '".$edate."')) and is_deleted = 'FALSE'");
        else
            $query = $this->db->query("select * from sur_survey where ((start_date <='".$sdate."' and end_date >='".$sdate."') or (start_date <='".$edate."' and end_date >='".$edate."')) and survey_id !='".$sid."' and is_deleted = 'FALSE'");
		return $query->num_rows() == 0 ? FALSE : TRUE;
    }
}
?>